package com.woshengqile.plugin.localStorage;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import com.jfinal.core.JFinal;
import com.woshengqile.Setting;
import com.woshengqile.plugin.StoragePlugin;
import com.woshengqile.util.SystemUtils;

/**
 * Plugin - 本地文件存储
 * 
 */
public class LocalStoragePlugin extends StoragePlugin {

	private ServletContext servletContext = JFinal.me().getServletContext();

	@Override
	public String getName() {
		return "本地文件存储";
	}

	@Override
	public String getVersion() {
		return "1.0";
	}

	@Override
	public String getAuthor() {
		return "JFinalShop";
	}

	@Override
	public String getSiteUrl() {
		return "http://www.jfinalshop.com";
	}

	@Override
	public String getInstallUrl() {
		return null;
	}

	@Override
	public String getUninstallUrl() {
		return null;
	}

	@Override
	public String getSettingUrl() {
		return "local_storage/setting";
	}

	@Override
	public void upload(String path, File file, String contentType) {
		try {
			File destFile;
			Setting setting = SystemUtils.getSetting();
			if (StringUtils.isEmpty(setting.getLocalStoragePath())) {
				destFile = new File(servletContext.getRealPath(path)); 
			} else {
				destFile = new File(setting.getLocalStoragePath() + path);
			}
			FileUtils.moveFile(file, destFile);
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	@Override
	public String getUrl(String path) {
		return path;
	}

}